// 商户Mock数据 - 基于 t_merchant_info 表结构
export interface Merchant {
  merchantId: string      // 商户ID(格式: M+13位时间戳)
  merchantName: string    // 商户名称
  providerId: string      // 所属服务商
  status: number          // 状态：0-停用，1-启用，2-审核中，3-审核失败
  contactName: string     // 联系人姓名
  contactTelephone: string // 联系人手机号
  remark: string | null   // 备注
  createdTime: string     // 创建时间
  updatedTime: string     // 更新时间
}

export const merchants: Merchant[] = [
  {
    merchantId: 'M1746866325792',
    merchantName: '华东收银测试',
    providerId: 'SP1752154718336', // 火金科技
    status: 1,
    contactName: '陈绪爱',
    contactTelephone: '13800138001',
    remark: '测试商户 - 软件开发行业',
    createdTime: '2024-01-01 10:00:00',
    updatedTime: '2024-01-15 10:00:00'
  },
  {
    merchantId: 'M1746866425893',
    merchantName: '惊喜来干货',
    providerId: 'SP1752154718337', // 田园网络
    status: 1,
    contactName: '陈亮',
    contactTelephone: '13800138002',
    remark: '电子商务行业',
    createdTime: '2024-01-02 10:00:00',
    updatedTime: '2024-01-16 10:00:00'
  }
]

// 商户状态枚举 - 基于数据库表定义
export const MerchantStatus = {
  DISABLED: 0,         // 停用
  ENABLED: 1,          // 启用
  PENDING: 2,          // 审核中
  FAILED: 3            // 审核失败
}

// 获取商户状态名称
export const getMerchantStatusName = (status: number): string => {
  const statusMap: { [key: number]: string } = {
    0: '停用',
    1: '启用',
    2: '审核中',
    3: '审核失败'
  }
  return statusMap[status] || '未知'
}

// 获取商户状态颜色
export const getMerchantStatusColor = (status: number): string => {
  const colorMap: { [key: number]: string } = {
    0: 'default',
    1: 'success',
    2: 'processing',
    3: 'error'
  }
  return colorMap[status] || 'default'
}

// 根据商户ID获取商户名称
export const getMerchantName = (merchantId: string): string => {
  const merchant = merchants.find(m => m.merchantId === merchantId)
  return merchant ? merchant.merchantName : merchantId
}

// 生成新的商户ID
export const generateMerchantId = (): string => {
  return 'M' + Date.now().toString()
}

// 根据条件筛选商户
export const filterMerchants = (filters: {
  merchantName?: string
  providerId?: string
  status?: number
}) => {
  return merchants.filter(merchant => {
    if (filters.merchantName && !merchant.merchantName.toLowerCase().includes(filters.merchantName.toLowerCase())) {
      return false
    }
    if (filters.providerId && merchant.providerId !== filters.providerId) {
      return false
    }
    if (filters.status !== undefined && merchant.status !== filters.status) {
      return false
    }
    return true
  })
}

// 模拟API - 获取商户列表
export const mockLoadMerchants = (params: any) => {
  const { current = 1, pageSize = 10, merchantName, providerId, status } = params

  let filteredData = merchants

  if (merchantName) {
    filteredData = filteredData.filter((m) => m.merchantName.includes(merchantName))
  }
  if (providerId) {
    filteredData = filteredData.filter((m) => m.providerId === providerId)
  }
  if (status !== undefined && status !== null) {
    filteredData = filteredData.filter((m) => m.status === Number(status))
  }

  const total = filteredData.length
  const start = (current - 1) * pageSize
  const end = start + pageSize
  const paginatedData = filteredData.slice(start, end)

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        data: paginatedData,
        total: total
      })
    }, 500)
  })
}

// 模拟API - 保存商户
export const mockSaveMerchant = (merchant: Merchant) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        success: true,
        message: '保存成功'
      })
    }, 500)
  })
}

// 模拟API - 删除商户
export const mockDeleteMerchant = (merchantId: string) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        success: true,
        message: '删除成功'
      })
    }, 500)
  })
}